#! /usr/bin/python
# -*- coding: utf-8 -*-

import propertier
import getopt
import sys
import os.path

__author__="miron"
def usage(info = ""):
    """Print usage info"""
    if info != "":
        print(info)
    print("Usage: caker -n ProjectName [[-d path] [-a -c -o -m -b] | -f fileName]")
    print("  -n Name of project")
    print("  -d path for cake app folder, '.' by default")
    print("  -a scan all files")
    print("  -c scan controllers")
    print("  -o scan components")
    print("  -m scan models")
    print("  -b scan behaviors")
    print("  -f File to scan")
    print("  -h This help message")

    print(" Author: Miron Pawlik")


def main(argv):
    """Main function"""
    path = '.'
    projectName = ''
    fileName = ''
    toScan = set()

    try:
        opts, args = getopt.getopt(argv, "f:n:d:acomb")
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt == "-h":
            usage()
            sys.exit()
        elif opt == '-d':
            path = arg
        elif opt == '-n':
            projectName = arg
        elif opt == '-f':
            fileName = arg
        elif opt == '-a':
            toScan = set(['controllers', 'components', 'models', 'behaviors'])
        elif opt == '-c':
            toScan.add('controllers')
        elif opt == '-o':
            toScan.add('components')
        elif opt == '-m':
            toScan.add('models')
        elif opt == '-b':
            toScan.add('behaviors')
    if projectName == '' or len(toScan) == 0:
        usage("Missing project name or no types to scan")
        sys.exit(3)

    if fileName != '': #if file name was given
        if len(toScan) != 1:
            print("You have to choose one type of file")
            sys.exit(4)
        elif not os.path.isfile(fileName):
            print("File do not exists")
        else:
            if 'controllers' in toScan:
                p = propertier.Propertier('./', ['uses', ('components', 'Component')], {'package' : projectName, 'subpackage' : 'controller'})
            elif 'components' in toScan:
                p = propertier.Propertier('./', ['uses', ('components', 'Component')], {'package' : projectName, 'subpackage' : 'controller.components', 'property': 'AppController $controller'})
            elif 'models' in toScan:
                p = propertier.Propertier('./', ['hasMany', 'belongsTo', 'hasOne'], {'package' : projectName, 'subpackage' : 'model'})
            elif 'behaviors' in toScan:
                p = propertier.Propertier('./', [], {'package' : projectName, 'subpackage' : 'models.behaviors'})

            p.read_file(fileName);
    else: #if we want to scan whole cake project
        if not os.path.isdir(path + '/config'):
            print(path + " is not correct cake path")
            sys.exit(5)
        if 'controllers' in toScan:
            p = propertier.Propertier(path + '/controllers/', ['uses', ('components', 'Component')], {'package' : projectName, 'subpackage' : 'controller'})
            p.read_dir()
            p.read_file('../app_controller.php');
        if 'components' in toScan:
            p = propertier.Propertier(path + '/controllers/components/', ['uses', ('components', 'Component')], {'package' : projectName, 'subpackage' : 'controller.components', 'property': 'AppController $controller'})
            p.read_dir()

        if 'models' in toScan:
            p = propertier.Propertier(path + '/models/', ['hasMany', 'belongsTo', 'hasOne'], {'package' : projectName, 'subpackage' : 'model'})
            p.read_dir()
            p.read_file('../app_model.php');
        if 'behaviors' in toScan:
            p = propertier.Propertier(path + '/models/behaviors/', [], {'package' : projectName, 'subpackage' : 'models.behaviors'})
            p.read_dir()


    
if __name__ == "__main__":
    main(sys.argv[1:])

